<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="150px">
      <el-form-item label="分类名称" prop="categoryName">
        <el-input v-model="form.categoryName" placeholder="分类名称" maxlength="30" class="col"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input type="number" placeholder="排序" v-model="form.sort" class="col" maxlength="5"/>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:merchantActivities:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { saveClassification} from "@/api/merchantActivity";
import {list} from "@/api/business";

export default {
  components: {},
  props: {
    tableData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    id: {
      default: null
    },
    businessId: {
      default: null
    }
  },
  data() {
    return {
      form: {
        categoryName: '',
        businessId: 0,
        sort: 0,

      },
      rules: {
        categoryName: [{required: true, message: '请填写分类名称', trigger: ['blur']}],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
    }
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    getBusinessList() {
      list({page: 1, limit: 5000}).then(res => {
        this.businessList = res.list
      })
    },
    getDetail() {
      if (this.tableData) {
        this.form = this.tableData
      }
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        this.form.businessId = this.businessId;
        saveClassification(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideClassificationDialog')
    }
  }
}
</script>
<style lang="scss" scoped>

.col {
  width: 520px;
}

.img {
  width: 120px;
  height: 120px;
  margin-right: 10px;
}
</style>
